package com.cloudcc.mobile.view.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.TaskManagementAdapter;
import com.cloudcc.mobile.dao.impl.ApprovalEngineImpl;
import com.cloudcc.mobile.entity.approval.TaskManagementModel;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.ViewUtils;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.weight.EmptyLayout;
/**
 * 审批(待办任务)
 * @author lhy
 * @date 2015/12/08/12:00
 */
public class TaskManagementActivty extends BaseActivity implements OnClickListener{
	private TaskManagementAdapter adapter;
	private ArrayList<TaskManagementModel> tasklist;
	private ApprovalEngineImpl engine = new ApprovalEngineImpl();
	private String userId;
	private EmptyLayout empty_loading;
	@Bind(R.id.bar_back)
	ImageView back;
	@Bind(R.id.bar_title_text)
	TextView title;
	@Bind(R.id.bar_button_plus)
	ImageView imgplus;
	@Bind(R.id.task_management_list)
	ListView listview;
	@Bind(R.id.management_list_loadmore)
	PtrClassicFrameLayout mptrframe;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		back.setOnClickListener(this);
		
		imgplus.setVisibility(View.GONE);
		adapter = new TaskManagementAdapter(this);
		listview.setAdapter(adapter);
		initEmpty();
		userId = UserManager.getManager().getUser().userId;
		title.setText("待审批");
		initframe();
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int point,
					long arg3) {
				Intent intent2 = new Intent(TaskManagementActivty.this, TaskManagementItemActivty.class);
				TaskManagementModel model = (TaskManagementModel) listview.getAdapter().getItem(point);
				Bundle b = new Bundle();
				b.putSerializable("task", model);
				intent2.putExtras(b);
				startActivityForResult(intent2, 0);
			}
			
		});

	}
	private void initEmpty() {
		empty_loading = new EmptyLayout(mContext);
		empty_loading.setLayoutParams(new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		ViewUtils.addListViewEmptyView(mContext, listview, empty_loading);
		empty_loading.empty();
	}
	public void initframe(){
		mptrframe.setLastUpdateTimeRelateObject(this);
		mptrframe.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listview, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
            	initdata();
            }
        });
		mptrframe.setLoadingMinTime(1000);
		mptrframe.setDurationToClose(200);
		mptrframe.setDurationToCloseHeader(1000);
		mptrframe.setResistance(1.7f);
		mptrframe.setRatioOfHeaderHeightToRefresh(1.2f);
		mptrframe.setPullToRefresh(false);
		mptrframe.setKeepHeaderWhenRefresh(true);
		mptrframe.postDelayed(new Runnable() {
            @Override
            public void run() {
            	mptrframe.autoRefresh();
            }
        }, 150);
	}
	public void initdata(){
		tasklist = new ArrayList<TaskManagementModel>();
		engine.GetManagementTask(userId, new ResultCallBack<List<TaskManagementModel>>() {
			
			@Override
			public void onSuccess(List<TaskManagementModel> t) {
				for (TaskManagementModel approvalModel : t) {
					tasklist.add(approvalModel);
				}
				mptrframe.refreshComplete();
				if(tasklist.size() <1){
					empty_loading.empty();
				}
				adapter.changeData(tasklist);
				
			}
			
			@Override
			public void onFailure(ErrorInfo error) {
				mptrframe.refreshComplete();
				Toast.makeText(TaskManagementActivty.this, "获取任务失败，请重试", Toast.LENGTH_SHORT).show();
				
			}
		});
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	@Override
	public int getLayoutId() {
		return R.layout.taskmanagement;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bar_back:
			TaskManagementActivty.this.finish();
			break;
		default:
			break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		mptrframe.autoRefresh();
	}
	

}
